import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
///时间选择器
class WidgetTemperature extends StatefulWidget{

  final TextEditingController fastController;
  final TextEditingController lastController;

  const WidgetTemperature({Key key, this.fastController, this.lastController}) : super(key: key);


  @override
  _widgetTemperature createState()=>_widgetTemperature();

}

class _widgetTemperature extends State<WidgetTemperature>{

  DateTime initialDate;
  DateTime dateTime;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //设置默认显示的日期为当前
    initialDate = DateTime.now();


    String time='${initialDate.year}-${initialDate.month}-${initialDate.day}\t${initialDate.hour}:${initialDate.minute}';
    widget.fastController.text=time;
    widget.lastController.text=time;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.only(left: 10.0,top: 10.0,right: 10.0,bottom: 5.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            getFastTimeText(context),
            Container(
              alignment: Alignment.center,
              width: ScreenUtil().setWidth(30.0),
              child: Text('-'),
            ),
            getLastTimeText(context),
          ],
      ),
    );
  }

  //开始时间框
  Widget getFastTimeText(BuildContext context){
    return InkWell(
      onTap: (){
        showDefaultYearPicker(context,true);
      },
      child: Container(
        alignment: Alignment.center,
        height: ScreenUtil().setHeight(50.0),
        width: ScreenUtil().setWidth(280.0),
        decoration: BoxDecoration(
            border: Border.all(
                color: Colors.blue,
                width: 0.5
            ),
            borderRadius: BorderRadius.all(Radius.circular(5.0))
        ),
        child: TextField(
          controller: widget.fastController,
          autofocus: false,
          autocorrect: false,
          enabled: false,
          decoration: InputDecoration(
            contentPadding: EdgeInsets.only(top:3.0,left: 10.0),
            border: InputBorder.none
          ),
        ),
      ),
    );
  }


  //结束时间框
  Widget getLastTimeText(BuildContext context){
    return InkWell(
      onTap: (){
        showDefaultYearPicker(context,false);
      },
      child: Container(
        alignment: Alignment.center,
        height: ScreenUtil().setHeight(50.0),
        width: ScreenUtil().setWidth(280.0),
        decoration: BoxDecoration(
            border: Border.all(
                color: Colors.blue,
                width: 0.5
            ),
            borderRadius: BorderRadius.all(Radius.circular(5.0))
        ),
        child: TextField(
          controller: widget.lastController,
          autofocus: false,
          autocorrect: false,
          enabled: false,
          decoration: InputDecoration(
              contentPadding: EdgeInsets.only(top:3.0,left: 10.0),
              border: InputBorder.none
          ),
        ),
      ),
    );
  }


  //选择时间 fastTime是否开始时间
  void showDefaultYearPicker(BuildContext context,bool fastTime) async {
      dateTime = await showDatePicker(
      context: context,
      //定义控件打开时默认选择日期
      initialDate: initialDate,
      //定义控件最早可以选择的日期
      firstDate: DateTime(2018, 1),
      //定义控件最晚可以选择的日期
      lastDate: DateTime(2022, 1),
      builder: (BuildContext context, Widget child) {
        return Theme(
          data:ThemeData(
            accentColor: Colors.blue,
          ),
          child: child,
        );
      },
    );
    if (dateTime != null && dateTime != initialDate) {
      print(dateTime);


      showDefaultDatePicker(context,fastTime);
    }
  }


  //设置显示显示的时间为当前

  void showDefaultDatePicker(BuildContext context,bool fastTime) async {
    TimeOfDay initialTime = TimeOfDay.now();
    final TimeOfDay timeOfDay = await showTimePicker(
      context: context,
      initialTime: initialTime,
      builder: (BuildContext context, Widget child) {
        return Theme(
          data: ThemeData(
            accentColor: Colors.blue,
          ),
          child: child,
        );
      },
    );
    if (timeOfDay != null) {
      setState(() {
        initialTime = timeOfDay;
        if(fastTime){
         //开始时间
          widget.fastController.text= '${dateTime.year}-${dateTime.month}-${dateTime.day} ${initialTime.hour}:${initialTime.minute}';//-${initialDate.month}-${initialDate.day}\t${initialTime.hour}:${initialTime.minute}
        }else{
          //结束时间
          widget.lastController.text= '${dateTime.year}-${dateTime.month}-${dateTime.day}\t${initialTime.hour}:${initialTime.minute}';
        }
      });
    }
  }

}

